<?php
/**
 * [LICENCE]
 */

require_once 'Madoqua/View/Helper/Abstract.php';

/**
 * View helper to display errors/exceptions
 * @category   Madoqua
 * @package    Madoqua_View
 * @subpackage Helper
 * @author     Graham Anderson - graham@andtech.eu
 * @copyright  Copyright (c) 2008
 * @todo work some kind of non intrusive behaviour into the flow of page rendering
 */
class Madoqua_View_Helper_Error extends Madoqua_View_Helper_Abstract
implements Madoqua_Observer_Interface_Exception
{

    public function __construct()
    {
        $this->_view = Zend_Layout::getMvcInstance()->getView();       
    }
    
    /**
     * Failure, doh!
     * @param Madoqua_Exception $e 
     * @see Madoqua_Observer_Interface_Exception::reThrow()
     */
    function reThrow(Madoqua_Exception $e)
    {
        throw $e;
    }

    /**
     * Pass the error/exception details to the view.
     * @param Madoqua_Exception $e 
     * @see Madoqua_Observer_Interface_Exception::handle()
     * @return void
     */
    public function handle(Madoqua_Exception $e)
    {
        $layout = Zend_Layout::getMvcInstance();
        if (!$layout->getMvcEnabled()) {
            $this->reThrow($e);    
        }
        
        if (!isset($this->_view->errors)) {
            $errors = array();
            $this->_view->errors = $errors;
        }
        $this->_view->errors[] = $e;        
    }
    
    /**
     * Proxy the hook to the handling method
     * @param Exception $e
     * @return Madoqua_View_Helper_Error $this
     */
    public function error(Exception $e)
    {
        $this->handle($e);
        return $this;   
    }
}